Functions, Prototypes and Function Calls


Overview

Like C, Pog scripts are built up of functions. Functions can be exported from a script for use in another script, allowing the Pog language to be extended via scripted packages.

All variables defined in a function are local to that function, and cannot be accessed outside that function, except via a return statement.

 

Prototyping a Function

To make full use of a function in a script, you must prototype it first. This informs the compiler of the name, type and arguments of a function before the function is properly defined.

The prototype definition should be placed in the Pog script between the exports / enumerators section, and the script code block. See Pog Script Structure for details of where prototypes are defined.

The prototype takes the same form as the function header (detailed below), with the keyword prototype at the beginning. e.g:
prototype iExample.PrintHello( int number_of_times );

 

 

Defining a Function

A function consists of a header and a code block. The header contains the function definition, including the function name and its parameters, while the code block contains the actual code executed when the function is called.

 

Function Header

A function header takes the following form:
{ prototype } { task / return_type } { package_name }.identifier ( { formal_argument_list } )

An example:
int iExample.PrintHello( int number_of_times );

prototype
Used for prototyping a function.

task
This parameter is used to define multitasking functions. See Multitasking for more details. Task functions cannot return a value, as they return their task handle instead.

return_type
Non task functions can return a value, and their return type, i.e. bool, int, float, etc. must be specifed here if they do.

package_name
This is the name of the package. If the function being defined / used is located in the current script package, this parameter can be omitted.

identifier
This is the function name. By convention exported functions use upper and lower case names with no underscores, while non-exported functions use lower case names with underscores.

Example of exported function name
PrintHello()


Example of non-exported Function name.
print_hello()

 

formal_argument_list
This contains the comma separated list any parameters for the function. Not every function requires parameters. Some  Any parameters defined here do not have to be defined in the function code block. The type and name of each parameter must be specified.

Example Parameters:
int count, float time, string name

 

Function Code Block

The function code block defines the code for the function. 

Declaring Variables
Any variables used in the function must be declared at the start of the code block e.g:

my_function()
{
	int count = 1;
	Debug.PrintString( "Hello\n");
	Debug.PrintInt( count );
}

 

Returning Values
Non task functions can return a value. The value can be a variable, constant, expression, enumeration, or a handle. To return a value use the return statement. e.g.

return count;

Functions that return a value must specify the return value type in their prototype and function headers, e.g.:

prototype int my_function();
int my_function()
{
	count = 1
	return count;
}

 

Exporting Functions

To use a function in another script you must export it by placing its function name in the provides section of the Pog script, and also provide its definition in the script's header file. See Pog Script Structure and Header Files for details.

Calling functions

Task Functions
Task functions are a special case and must be called using the start command. See Multitasking for details.

Local Functions
To call a function in the script where it is defined, simply use the function name without the package name prefix. e.g.:

my_function();

Imported Functions
To use a function from a native code package or a different script package you must specify the package name of the function as well as the function name. Note that you must also specify any packages whose functions you wish to use in the uses section of the Pog script. See Pog Script Structure for details.

Example of an imported function call:

iExample.ImportedFunction();